//  JSManager.pp
//
//  SweetpomeII
//  THIS IS NOT A OPEN SOURCE PROJECT!
//
//  Created by cc on 12-5-16.
//  Copyright (c) 2009-2012 Chukong Technologies, Inc. 触控科技 版权所有
//  All rights reserved.
//

#include "JSManager.h"
#include "CCFileUtils.h"

namespace SP_JSBind
{
	JSClass globalClass =
	{
		"Global",
		0,
		JS_PropertyStub,
		JS_PropertyStub,
		JS_PropertyStub,
		JS_PropertyStub,
		JS_EnumerateStub,
		JS_ResolveStub,
		JS_ConvertStub,
		JS_FinalizeStub,
	} ;
    
	/**
	 *	@brief	error reporter function. user can change this function.
	 *
	 *	@param 	cx 	js context.
	 *	@param 	msg 	error message.
	 *	@param 	report 	error info.
	 */
	void Default_Error_Reporter(JSContext* cx, const char* msg, JSErrorReport* report)
	{
        __ENTER_FUNCTION
        //		::printf("JS Default_Error_Reporter [%s] [%u] [%s]\n",
        //				report->filename ? report->filename : "<no filename>", (unsigned int) report->lineno, msg) ;
        __EXIT_FUNCTION("Default_Error_Reporter\n")
	}
    
    static JSBool print( JSContext* cotext, JSObject* object, uintN argc, jsval* argv, jsval* value )
    {
        int i = 0;
        JSString* jss;
        for( i = 0; i < argc; ++i )
        {
            jss = JS_ValueToString(cotext, argv[ i ] );
            printf("Message from script environment : %s \n", JS_GetStringBytes( jss ) );
        }
        return JS_TRUE;
    }
    
    static JSBool Require( JSContext* cotext, JSObject* object, uintN argc, jsval* argv, jsval* value )
    {
        JSString* jss = JS_ValueToString(cotext, argv[ 0 ] );
        std::string jsPath = cocos2d::CCFileUtils::fullPathFromRelativePath(JS_GetStringBytes( jss ));
        
        CJSManager::Instance()->ExecuteScriptFromFile(jsPath.c_str());
        return JS_TRUE;
    }
    
    static JSBool Create( JSContext* cotext, JSObject* object, uintN argc, jsval* argv, jsval* value )
    {
        JSString* jss = JS_ValueToString(cotext, argv[ 0 ] );
        std::string jsPath = cocos2d::CCFileUtils::fullPathFromRelativePath(JS_GetStringBytes( jss ));
        
        CJSManager::Instance()->ExecuteScriptFromFile(jsPath.c_str());
        return JS_TRUE;
    }
    //    //传void＊ 传错类型 或者传未导出的类都会崩溃
    /*
     这种方式可以将c++的指针传递到js
     
     Sweetpome::GameObject * gameobj = Sweetpome::ObjectFactory::getSingleton().getGameObject(JS_GetStringBytes(jss));
     *value = OBJECT_TO_JSVAL(gameobj->m_pJSObject);
     */
    //    static JSBool getGameObject( JSContext* cotext, JSObject* object, uintN argc, jsval* argv, jsval* value )
    //    {
    //        JSString *jss = JS_ValueToString(cotext, argv[0]);
    ////        Sweetpome::GameObject * gameobj = Sweetpome::ObjectFactory::getSingleton().getGameObject(JS_GetStringBytes(jss));
    ////        *value = OBJECT_TO_JSVAL(gameobj->m_pJSObject);
    //        return JS_TRUE;
    //    }
    
    //    //传void＊ 传错类型 或者传未导出的类都会崩溃
    static JSBool getRootGameObject( JSContext* cotext, JSObject* object, uintN argc, jsval* argv, jsval* value )
    {
        JSString *jss = JS_ValueToString(cotext, argv[0]);
        Sweetpome::GameObject * gameobj = Sweetpome::GameIOS::getSingleton().getRootGameObject();
        *value = OBJECT_TO_JSVAL(gameobj->m_pJSObject);
        return JS_TRUE;
    }
    
    
    static JSBool loadGameObjectTemplateFile( JSContext* cotext, JSObject* object, uintN argc, jsval* argv, jsval* value )
    {
        JSString *jss = JS_ValueToString(cotext, argv[0]);
        JsonReader::loadGameObjectTemplateFile(JS_GetStringBytes(jss));
        return JS_TRUE;
    }
    
    static JSBool createGameObjectFromTemplate( JSContext* cotext, JSObject* object, uintN argc, jsval* argv, jsval* value )
    {
        JSString *jss = JS_ValueToString(cotext, argv[0]);
        Sweetpome::GameObject * gameobj = GameObjectTemplateManager::getSingleton().createFromTemplate(JS_GetStringBytes(jss));
        *value = OBJECT_TO_JSVAL(gameobj->m_pJSObject);
        return JS_TRUE;
    }
    
    
    //    //传void＊ 传错类型 或者传未导出的类都会崩溃
    static JSBool getWindowRect( JSContext* cotext, JSObject* object, uintN argc, jsval* argv, jsval* value )
    {
        return JS_TRUE;
    }
    
    //最后必须要有个 0 
    JSFunctionSpec g_Functions[] = {
        { "print", print, 1, 0, 0 },
        { "Require", Require, 1, 0, 0 },
        //        { "GetGameObj", getGameObject, 1, 0, 0 },
        { "GetRootGameObj", getRootGameObject, 1, 0, 0 },
        { "LoadGameObjectTemplateFile", loadGameObjectTemplateFile, 1, 0, 0 },
        { "CreateGameObjectFromTemplate", createGameObjectFromTemplate, 1, 0, 0 },
        { "CreateGameObjectFromTemplate", createGameObjectFromTemplate, 1, 0, 0 },
        
        { 0 },
        
    };
    
	
	CJSManager* CJSManager::s_pManager = NULL ;
}













